#pragma once

#include "my_disk_volume.h"
#include "comm/my_defs.h"
#include "comm/my_atomic.h"

typedef struct my_disk_cache_s my_disk_cache_t;

typedef void(*my_disk_cache_open_done_cb)(my_disk_cache_t *disk, my_bool_t success);

struct my_disk_cache_s {
    // static configuration
    int                         level;
    const char                  *file_path;
    int64_t                     except_size;

    // context
    my_disk_cache_open_done_cb  done_cb;
    void                        *ctx;

    // dynamic resource
    int                         fd;

    int                         nvolumes;
    my_disk_volume_t            *volumes;       // nvolumes
    my_bool_t                   *open_stats;    // nvolumes
    my_atomic_int_t             ndone;
};

static inline void
my_disk_cache_init(my_disk_cache_t *disk_cache) {

    bzero(disk_cache, sizeof(my_disk_cache_t));
    disk_cache->fd = INVALID_FD;
    my_atomic_int_init(&disk_cache->ndone);
}

void
my_disk_cache_open(my_disk_cache_t *disk_cache, int level,
    const char *file_path, int64_t except_size,
    my_bool_t create_new, my_disk_volume_err_cb err_cb,
    my_disk_cache_open_done_cb done_cb, void *ctx);

void
my_disk_cache_notify_close(my_disk_cache_t *disk_cache,
    my_bool_t only_close_write);

// void
// my_disk_cache_wait_exit(my_disk_cache_t *disk_cache);